<?php
/*首页数据*/
namespace Admin\Model;
use Think\Model;
class CarsModel extends Model {
    public function getCarsData($data = ""){
        $car       = M('Cars');//汽车信息表
        $res       = $car->field('id,name,carnum,is_promo,number')->select();
        return $res;
    }

    /**
     * 查看详情
     */
    public function getCarsXiangQing($data){
    	  $user = M('user');//用户表
        $order= M('order');//订单表
        $cars = M('cars');//车辆表
        $city = M('city');//城市表
        $admin= M('admin');//管理员表
        $wayuser        = M('wayuser');//渠道表
        $carscolour     = M('cars_stock');//颜色表
        $stockcity_db   = M('stock');//库存所在地
        $orderoperation = M('order_operation');//操作表
        $price_db       = M('price');//价格表
        $pay            = M('order_month_pay');//记录表
        //==========================================
        $orderaf   = $order->where(array('orderid'=>$data['orderid']))->find();//订单
        $add       = $city->where(array('id'=>$orderaf['cityid']))->field('name,pid')->find();//省
        $kuncun    = $carscolour->where(array('id'=>$orderaf['stockid']))->find();
        $stockcity = $stockcity_db->where(array('id'=>$kuncun['sid']))->find();
        $colours   = $kuncun['color'];//颜色
        $addpid    = $city->where(array('id'=>$add['pid']))->field('name')->find();//市
        $userid	   = $user->where(array('id'=>$orderaf['uid']))->find();//用户
        $carsid    = $cars->where(array('carnum'=>$orderaf['carnum']))->field('name,guide_price,carnum,brand,type,details,month_pay,first_pay')->find();
        if($orderaf['create_time'] >  1527838056){
           $priceversion    = $price_db->where(array('id'=>$orderaf['price_version_id']))->field('month_pay,first_pay,pay_month,insurance,surplus_money')->find();
           $carsid['month_pay'] = $priceversion['month_pay'];
           $carsid['first_pay'] = $priceversion['first_pay'];
        }
        $stokid           = $city->where(array('id'=>$kuncun['cityid']))->field('name')->find();
        $m['orderid']     = array('EQ',$orderaf['orderid']);
        $m['status']      = array('EQ',0);
        $orderoperationid = $orderoperation->where($m)->find();//通过记录表查询到办单人员
       	$usernum          =  $admin->where(array('usernum'=>$orderoperationid['usernum']))->find();
        $beoverdue        = array();
        if($orderaf['status'] < 6){
        	// echo "订单未完成";
        	$beoverdue['overdueorder']  = "否"; //是否越期
        	$beoverdue['overduetime']   = "--"; //越期时间
          $beoverdue['overduewanjie'] = "--"; 
        	$beoverdue['overdday'] = "--";//逾期天数 
        }else{
        	$payarr = $pay -> where(array('orderid'=>$orderaf['orderid'],'status'=>0))->find();
        	if($payarr){
        		$datime = date('Ymd',$payarr['pay_time']);
	        	$jttime = date('Ymd',time());
	        	if($jttime > $datime){
	        		$beoverdue['overdueorder']  = "是";
	        	}else{
	        		$beoverdue['overdueorder']  = "否";
	        	}
            if($payarr['next_pay_time'] -(time()+24*60*60)>=0){
                $beoverdue['overdday']= ceil(($payarr['pay_time'] -time())/(24*60*60));
            }else{
                $beoverdue['overdday']=0-ceil((time()-($payarr['pay_time']+24*60*60))/(24*60*60));
            }
	        	$beoverdue['overduetime']   = date('Y-m-d',$payarr['pay_time']);
	        	$beoverdue['overduewanjie'] = $orderoperation->where(array('orderid'=>$orderaf['orderid'],'status'=>6))->find()['create_time'];
	        	}
	        	$beoverdue['overduewanjie'] = date('Y-m-d',$beoverdue['overduewanjie']);
        	  if($beoverdue['overdday'] >=0){
                $beoverdue['overdday'] = 0;
            }
        }
        
        if(!$userid['waynum']){
              $userid['waynum'] = "--";
        }
        $userid['orderid']      = $orderaf['orderid'];//订单号
        $userid['orderidstatus']= $orderaf['status'];//订单状态
        $userid['status']       = $orderaf['status'];//订单状态
        $userid['order_time']   = date('Y-m-d H:i',$orderaf['create_time']); //订单创建时间
        $userid['admin_name']   = $usernum['name'];//办单员姓名
        $userid['admin_num']    = $usernum['usernum'];//办单员编号
        $userid['pidname']      = $addpid['name'];//省
        $userid['cityname']     = $add['name'];  //市
        $userid['carnum']       = $carsid['carnum'];//车辆编号
        $userid['car_num']      = $orderaf['car_num'];//车牌号
        $userid['carname']      = $carsid['name']; //车辆名称
        $userid['carbrand']     = $carsid['brand']; //车辆品牌
        $userid['cartype']      = $carsid['type']; //车辆类型
        $userid['colours']      = $colours; //车辆颜色
        $userid['guide_price']  = $carsid['guide_price']; //车辆价格
        $userid['month_pay']    = $carsid['month_pay']; //车辆月供
        $userid['first_pay']    = $carsid['first_pay']; //车辆首付
        $userid['usertel']      = $userid['tel']; //客户手机号
        $userid['overdueorder']   = $beoverdue['overdueorder'];//是否越期
        $userid['overduetime']    = $beoverdue['overduetime'];//越期时间
        $userid['overduewanjie']  = $beoverdue['overduewanjie'];//订单完结时间
        $userid['overdday']       = $beoverdue['overdday'];//用户逾期天数
        $userid['details']        = $carsid['details'];//订单
        $userid['stok']           = $stockcity['city'];//车库所在地
        $userid['enginenum']      = $orderaf['enginenum'];//发动机号
        $userid['framenum']       = $orderaf['framenum'];//车架号
        // $userid['jdc_price']      = $carsid['jdc_price'];//筋斗价
        $userid['address']        = $orderaf['address'];//定位地址
        $userid['identify_url']  = serializeMysql($userid['identify_url'],1);
        $userid['driver_url']    = serializeMysql($userid['driver_url'],1);
        $userid['workproof_url'] = serializeMysql($userid['workproof_url'],1);
        $userid['bankflow_url']  = serializeMysql($userid['bankflow_url'],1);
        $userid['creditreport_url']  = serializeMysql($userid['creditreport_url'],1);
        $userid['otherdata_url']  = serializeMysql($userid['otherdata_url'],1);
        // var_dump($userid,'llll');die;
        return $userid;	
    }

    public function getCityData($data = "")
    {	
        $user     = M('user');//用户表
    	  $price_db = M('price');//价格表
        $order= M('order');//订单表
        $cars = M('cars');//车辆表
        $city = M('city');//城市表
        $admin= M('admin');//管理员表
        $wayuser= M('wayuser');//渠道表
        $carscolour= M('cars_stock');//颜色表
        $jdcstock= M('stock');//库存所在地
        $orderoperation = M('order_operation');//操作表
        $orderaf = $order->where(array('orderid'=>$data['orderid']))->find();
        $add     = $city->where(array('id'=>$orderaf['cityid']))->field('name,pid')->find();
        $kuncun  = $carscolour->where(array('id'=>$orderaf['stockid']))->find();//库存
        $colours = $kuncun['color'];//颜色
        $stockku = $jdcstock->where(array('id'=>$kuncun['sid']))->find();
        $stokid  = $city->where(array('id'=>$kuncun['cityid']))->field('name')->find();
        $addpid  = $city->where(array('id'=>$add['pid']))->field('name')->find();
        $userid	 = $user->where(array('id'=>$orderaf['uid']))->find();
        $carsid	 = $cars->where(array('carnum'=>$orderaf['carnum']))->field('jdc_price,name,guide_price,carnum,brand,type,month_pay,first_pay,details')->find();
        if($orderaf['create_time'] >  1527838056){
           $priceversion    = $price_db->where(array('id'=>$orderaf['price_version_id']))->field('month_pay,first_pay,pay_month,insurance,surplus_money')->find();
           $carsid['month_pay'] = $priceversion['month_pay'];
           $carsid['first_pay'] = $priceversion['first_pay'];
        }
        $m['orderid'] = array('EQ',$orderaf['orderid']);
        $m['status']  = array('EQ',0);
        $orderoperationid = $orderoperation->where($m)->order('id DESC')->find();//通过记录表查询到办单人员
       	$usernum   =  $admin->where(array('usernum'=>$orderoperationid['usernum']))->find();
       	$wayuserid =  $wayuser->where(array('waynum'=>$userid['waynum']))->find();
       	if($wayuserid){
       		$wayuserid['create_time'] = date('Y-m-d H:i',$wayuserid['create_time']);
       		if($wayuserid['status'] == "1"){
       			$wayuserid['status'] = "渠道激活";
       		}elseif($wayuserid['status'] =="0"){
       			$wayuserid['status'] = "渠道关闭";
       		}
            $userresadd = $user->where(array('waynum'=>$wayuserid['waynum']))->select();
            $count = count($userresadd);
            $where ="1=1";
            for($i = 0; $i < $count; $i++){
                if($sum){
                    $sum.=",'".$userresadd[$i]['id']."'";
                }else{
                    $sum.="'".$userresadd[$i]['id']."'";
                }
            }
            $where   .=" and uid in (".$sum.")";
            $beiju    = 0;
            $tuijian  = 0;
            $zongbeiju= 0;
            $orderarr = $order->where($where)->select();//总被拒单数
            foreach ($orderarr as $key => $value) {
                    $dtatetime  = date('Y-m',time());
                    $valuetime  = date('Y-m',$value['create_time']);
                    if($valuetime == $dtatetime){
                        if($value['status'] == "-1"){
                            $beiju += 1;
                        }
                        $tuijian += 1;
                    }
                    if($value['status'] == "-1"){
                        $zongbeiju += 1;
                    }
            }
            $wararr = $order->where($where)->order('id DESC')->limit(1)->select()[0];
            $wararr['create_time'] = date('Y-m-d H:i',$wararr['create_time']);
       	}else{
          $wayuserid['tel'] = "--";
          $userid['waynum'] = "--";
          $wayuserid['create_time'] = "--";
          $wararr['create_time'] = "--";
        }
        
        // var_dump($orderaf,'订单',$add,'城市',$addpid,'城市',$carsid,'车辆信息',$usernum,'admin员工',$wayuserid,'渠道',$userid,'用户信息',$colours,'颜色');die;
        $arr  = array(
              'orderid'        => $orderaf['orderid'],//订单号
              'order_time'     => date('Y-m-d H:i',$orderaf['create_time']), //订单创建时间
              'admin_name'     => $usernum['name'],//办单员姓名
              'admin_num'      => $usernum['usernum'],//办单员编号
              'waynum'         => $userid['waynum'],//渠道推荐码
              'pidname'        => $addpid['name'],//省
              'cityname'       => $add['name'],   //市
              'carnum'         => $carsid['carnum'],//车辆编号
              'carname'        => $carsid['name'], //车辆名称
              'carbrand'       => $carsid['brand'], //车辆品牌
              'cartype'        => $carsid['type'], //车辆类型
              'colours'        => $colours, //车辆颜色
              'guide_price'    => $carsid['guide_price'], //车辆价格
              'month_pay'      => $carsid['month_pay'], //车辆月供
              'first_pay'      => $carsid['first_pay'], //车辆首付
              'nickname'       => $userid['nickname'], //客户姓名
              'usertel'        => $userid['tel'], //客户手机号
              'workunit'       => $userid['workunit'],//客户工作单位
              'income'         => $userid['income'],  //客户收入
              'marriage'       => $userid['marriage'],//客户婚姻状况
              'creditcard'     => $userid['creditcard'],//客户信用卡
              'borrowing'      => $userid['borrowing'],//客户借款情况
              'bank'           => $userid['bank'],//银行名称
              'banker'         => $userid['banker'],//银行户主
              'banknum'        => $userid['banknum'],//银行卡号
              'identify_url'   => $userid['identify_url'],//身份证正反面
              'driver_url'     => $userid['driver_url'],//驾驶证
              'workproof_url'  => $userid['workproof_url'],//工作证明
              'bankflow_url'   => $userid['bankflow_url'],//银行流水
              'creditreport_url'=> $userid['creditreport_url'],//征信报告
              'otherdata_url'   => $userid['otherdata_url'],//补全资料
              'beiju'          => count($beiju),//本月被拒单数
              'tuijian'        => count($tuijian),//本月推荐单数
              'zongbeiju'      => count($zongbeiju),//总被拒单数
              'zongtuijian'    => count($orderarr),//总被拒单数
              'waytime'        => $wayuserid['create_time'],//渠道激活时间
              'waytel'         => $wayuserid['tel'],//渠道手机号
        	    'waycreatetime'  => $wararr['create_time'],//渠道上次办单时间
              'details'        => $carsid['details'],//订单完结时间
              'stok'           => $stockku['city'],//车库所在地
              // 'jdc_price'      => $carsid['jdc_price'],//筋斗价
              'usertype'       => $userid['usertype'],
              'contacts_name'       => $userid['contacts_name'],//紧急联系人
              'contacts_phone'       => $userid['contacts_phone'],//紧急联系人
              'contacts_bearing'       => $userid['contacts_bearing'],//紧急联系人
              'contacts_phone_tel'       => $userid['contacts_phone_tel'],//紧急联系人
              'contacts_user_name'       => $userid['contacts_user_name'],//紧急联系人
              'contacts_sip_bearing'       => $userid['contacts_sip_bearing'],//紧急联系人
        	);
          // var_dump($arr,'llll');die;
        return $arr;
    }
    /**
     * 汽车信息
     * id 汽车编号
     * field。表字段
     */
    public function OneCarData($id,$field) {
      $car_mdb = M('cars');
      $field= implode(',', $field);
      $car_data  =$car_mdb->where($id)->field($field)->find();
      return $car_data;
    }
}
